import axios from 'axios'
import { useUserStore } from '@/stores/user'

// 创建axios实例
const httpInstance = axios.create({
    baseURL: "/api",
    timeout: 5000
})

// axios请求拦截器
httpInstance.interceptors.request.use(config => {
    const token = useUserStore().userInfo.token
    if (token) {
        // 将token挂载到实例上
        config.headers.Authorization = `Bearer ${token}`
    }
    return config
}, e => Promise.reject(e))

// axios响应式拦截器
httpInstance.interceptors.response.use(res => res.data, e => {
    return Promise.reject(e)
})


export default httpInstance